//
//  EmoticonPackage.swift
//  EmoticonKeyBoard
//
//  Created by 周先生 on 15/11/23.
//  Copyright © 2015年 周先生. All rights reserved.
//

import UIKit

class EmoticonPackage: NSObject {

    //表情数组
    lazy var emoticonsList = [Emotion]()
    //表情分组的包名字
    var id: String?
    //表情分组的中文名字
    var group_name_cn: String?
    /**
     每20个按钮需要添加一个删除按钮
     页面表情不足21个需要补充
    */
    init(id: String, name: String, emotions: [[String: String]]){
        self.id = id
        self.group_name_cn = name
        super.init()
        //将表情字典数组 转换成模型数组
        
        //定义标记
        var index = 0
        
        //遍历字典数组
        for item in emotions {
        //拿到单个表情字段
            //字典转模型
            let e = Emotion(id: id, dict: item)
            //添加到数组
            emoticonsList.append(e)
            index++
            if index == 20 {
            //添加删除模型
                let deleteEmoticon = Emotion(isRemove: true)
                emoticonsList.append(deleteEmoticon)
                
                //这句很重要 恢复标记
                index = 0
                
            }
        }
        //不足21个表情 补充空表情
        insertEmptyEmoticon()
    }

    private func insertEmptyEmoticon() {
        let count = emoticonsList.count % 21
        if count == 0 {
        //刚好21个表情
            return
        
        }
        for _ in count..<20 {
            //添加空表情模型
            let empty = Emotion(isEmpty: true)
            emoticonsList.append(empty)
        }
        //最后一个 添加删除按钮
        let deleteEmoticon = Emotion(isRemove: true)
        emoticonsList.append(deleteEmoticon)
    }
    //重写description方法
    override var description: String {
    
    return "id = \(id), name = \(group_name_cn)"
    }
}
